<?php
            
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 抖音商城项目
 * Time: 2021/11/15 3:02 下午
 * Email: 17854288794@163.com
 */
class Setting_Page extends PluginWebPage {
    function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 7;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $item['tuan'] = json_decode($item['tuan'],true);
        $item['order'] = json_decode($item['order'],true);
        $item_order_count = count($item['order']);
        $now = time();

        $name = isset($_P['name'])?trim($_P['name']):'';
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;
        $shangjiaid = isset($_P['shangjiaid'])?$_P['shangjiaid']:0;
        $shangjia = BeilaiModel::getShangjiaList();
        array_unshift($shangjia,['id'=>0,'name'=>'全部商家']);
        $gifts = BeilaiModel::getGiftList($themeid,$shangjiaid,$name,0,0);
        $list = BeilaiModel::getGiftList($themeid,$shangjiaid,$name,$page_start,$page_size);
        $coupon_lists = BeilaiModel::getCouponList($themeid,$shangjiaid,$name,0,0);
        $coupon_list = BeilaiModel::getCouponList($themeid,$shangjiaid,$name,$page_start,$page_size);
        //最早一条优惠券记录
        $first_coupon_item = BeilaiModel::getFirstCoupon($themeid);
        $other_config = json_decode($item['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];
        $open_position = $other_config['open_position'];
        $open_shangjia_video = $other_config['open_shangjia_video'];
        $course_mode = $other_config['course_mode'];
        $shangjia_select_max = $other_config['shangjia_select_max'];

        $sec_bk_c1 = $other_config['sec_bk_c1'];
        $sec_bk_c2 = $other_config['sec_bk_c2'];
        $sec_txt_c = $other_config['sec_txt_c'];
        $sec_txt_size = $other_config['sec_txt_size'];
        $bl_buy_history = $other_config['bl_buy_history'];
        $gift_unlock_min_num = $other_config['gift_unlock_min_num'];
        $gift_unlock_max_num = $other_config['gift_unlock_max_num'];
        $lock_gift_mode = $other_config['lock_gift_mode'];
        $lock_gift_type = $other_config['lock_gift_type'];

        $gift_none_btn_txt = $other_config['gift_none_btn_txt']?$other_config['gift_none_btn_txt']:'免费获取';
        $gift_has_btn_txt = $other_config['gift_has_btn_txt']?$other_config['gift_has_btn_txt']:'已获取';
        $gift_refund_btn_txt = $other_config['gift_refund_btn_txt']?$other_config['gift_refund_btn_txt']:'已失效';
        $show_course_flag = $other_config['show_course_flag']?$other_config['show_course_flag']:0;
        $pn_wxapp = $_S['pn_wxapp'];
        if (!db_fieldexists(MODULE_NAME.'_shangjia_coupon','bind_price')){
            $sql = "ALTER TABLE `yx_".MODULE_NAME."_shangjia_coupon` ADD COLUMN `bind_price`  int(4)  not null default 1 comment '优惠券绑定金额';";
            db_query($sql);
        }
        $total = $page_total = count($coupon_lists);
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/beilai/setting/main', $return_clicent);
    }

    function edit()
    {
        return $this->post();
    }
    protected function post()
    {
        global $_G, $_P,$_S,$_C;
        $action = 6;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }

        if ($_G['ispost'] && $themeid ) {
            $op = $_P['op'];
            $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
            //            通用化设置
            if ($op=='common_setting'){
                $shangjia_rules = [];
                $shangjia_rule_ids = $_P['shangjia_rule_id'];
                $shangjia_rule_sort = $_P['shangjia_rule_sort'];
                $shangjia_rule_content = $_P['shangjia_rule_content'];
                $shangjia_rule_special = $_P['shangjia_rule_special'];
                if (is_array($shangjia_rule_ids)){
                    foreach ($shangjia_rule_ids as $k =>$v){
                        $dd = ['shangjia_rule_id'=>$v,'rule_content'=>$shangjia_rule_content[$k],'rule_sort'=>$shangjia_rule_sort[$k],'rule_special'=>$shangjia_rule_special[$k]];
                        $shangjia_rules[] = $dd;
                    }
                }
                if ($_S['beilai_style']==1){
                    $school_other_name = isset($_P['school_other_name'])?$_P['school_other_name']:'校区';
                    $course_other_name = isset($_P['course_other_name'])?$_P['course_other_name']:'课程';
                    $gift_other_name = isset($_P['gift_other_name'])?$_P['gift_other_name']:'赠品';
                }else{
                    $school_other_name = '校区';
                    $course_other_name = '课程';
                    $gift_other_name = '赠品';
                }
                $open_position = intval($_P['open_position']);
                if (!$_P['open_shangjia_rules']){
                    $course_mode = 0;
                }else{
                    $course_mode = $_P['course_mode'];
                }
                //二级分类下的设置
                $sec_bk_c1 = $_P['sec_bk_c1']?$_P['sec_bk_c1']:'#f82079';
                $sec_bk_c2 = $_P['sec_bk_c2']?$_P['sec_bk_c2']:'#fe878e';
                $sec_txt_c = $_P['sec_txt_c']?$_P['sec_txt_c']:'#000';
                $sec_txt_size = $_P['sec_txt_size']?(int)$_P['sec_txt_size']:16;
                if (!$sec_txt_size || $sec_txt_size<16 || $sec_txt_size>30){
                    $sec_txt_size = 16;
                }
                $gift_unlock_min_num = max(1,$_P['gift_unlock_min_num']);
                $gift_unlock_max_num = max(1,$_P['gift_unlock_max_num']);
                //礼品按钮提示语
                $gift_none_btn_txt = $_P['gift_none_btn_txt']?$_P['gift_none_btn_txt']:'免费获取';
                $gift_has_btn_txt = $_P['gift_has_btn_txt']?$_P['gift_has_btn_txt']:'已获取';
                $gift_refund_btn_txt = $_P['gift_refund_btn_txt']?$_P['gift_refund_btn_txt']:'已失效';
                //可选商家数
                $shangjia_select_max = isset($_P['shangjia_select_max'])?$_P['shangjia_select_max']:0;
                //显示课程统计数据
                $show_course_flag = isset($_P['show_course_flag'])?$_P['show_course_flag']:0;
                $other_config = json_encode(['school_other_name'=>$school_other_name,'course_other_name'=>$course_other_name,
                    'gift_other_name'=>$gift_other_name,
                    'sec_bk_c1'=>$sec_bk_c1,
                    'sec_bk_c2'=>$sec_bk_c2,
                    'sec_txt_c'=>$sec_txt_c,
                    'sec_txt_size'=>$sec_txt_size,
                    'open_shangjia_video'=>(int)$_P['open_shangjia_video'],
                    'gift_unlock_min_num'=>(int)$gift_unlock_min_num,
                    'gift_unlock_max_num'=>(int)$gift_unlock_max_num,
                    'lock_gift_type'=>(int)$_P['lock_gift_type'],
                    'lock_gift_mode'=>(int)$_P['lock_gift_mode'],
                    'gift_none_btn_txt'=>$gift_none_btn_txt,
                    'gift_has_btn_txt'=>$gift_has_btn_txt,
                    'gift_refund_btn_txt'=>$gift_refund_btn_txt,
                    'show_course_flag'=>$show_course_flag,
                    'bl_buy_history'=>$_P['bl_buy_history'],//显示购买记录
                    'open_position'=>$open_position,'course_mode'=>$course_mode,'shangjia_select_max'=>$shangjia_select_max]);

                $update_arr = [
                    'other_config'=>$other_config,
                    'poster_show'=>$_P['poster_show'],
                    'open_shangjia_rules'=>$_P['open_shangjia_rules'],
                    'shangjia_rules'=>json_encode($shangjia_rules),
                ];
                if (!$item){
                    $update_arr['themeid']=$themeid;
                    $res = BeilaiModel::insertReward($update_arr);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],$update_arr);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //我的底部图片设置
            if ($op=='btm_img_setting'){
                $btm_img = tomedia($_P['btm_img']);
                if (!$item){
                    $res = BeilaiModel::insertReward(['btm_img'=>$btm_img,'themeid'=>$themeid]);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['btm_img'=>$btm_img]);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //员工提成设置
            if ($op=='worker_money_setting'){
                $group_min_out_price = isset($_P['group_min_out_price'])?$_P['group_min_out_price']:0;
                $group_max_out_price = isset($_P['group_max_out_price'])?$_P['group_max_out_price']:0;
                $min_out_price = isset($_P['min_out_price'])?$_P['min_out_price']:0;
                $max_out_price = isset($_P['max_out_price'])?$_P['max_out_price']:0;
                if ($group_min_out_price>$group_max_out_price){
                    show_json(0,'团长提成不合法');
                    return;
                }
                if ($min_out_price>$max_out_price){
                    show_json(0,'团员提成不合法');
                    return;
                }
                if (!$item){
                    $res = BeilaiModel::insertReward(['group_min_out_price'=>$group_min_out_price,'group_max_out_price'=>$group_max_out_price,'min_out_price'=>$min_out_price,'max_out_price'=>$max_out_price,'themeid'=>$themeid]);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['group_min_out_price'=>$group_min_out_price,'group_max_out_price'=>$group_max_out_price,'min_out_price'=>$min_out_price,'max_out_price'=>$max_out_price]);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //购买设置
            if ($op=='buy_setting'){
                $course_min_num = $_P['course_min_num'];
                $course_max_num = $_P['course_max_num'];
                $gift_min_num = $_P['gift_min_num'];
                $gift_max_num = $_P['gift_max_num'];
                if ($course_min_num>$course_max_num){
                    show_json(0,'可选课程数不合法');
                    return;
                }
                if ($gift_min_num>$gift_max_num){
                    show_json(0,'可选赠品数不合法');
                    return;
                }
                $cg_data = [
                    'course_min_num'=>$course_min_num,
                    'course_max_num'=>$course_max_num,
                    'gift_min_num'=>$gift_min_num,
                    'gift_max_num'=>$gift_max_num
                ];
//                多价格
                if ($_S['bottom_jiage']>0){
                    $p2_course_min_num = $_P['p2_course_min_num'];
                    $p2_course_max_num = $_P['p2_course_max_num'];
                    $p2_gift_min_num = $_P['p2_gift_min_num'];
                    $p2_gift_max_num = $_P['p2_gift_max_num'];
                    if ($p2_course_min_num>$p2_course_max_num){
                        show_json(0,'拼团②可选课程数不合法');
                        return;
                    }
                    if ($p2_gift_min_num>$p2_gift_max_num){
                        show_json(0,'拼团②可选赠品数不合法');
                        return;
                    }
                    $cg_data['p2_course_min_num'] =$p2_course_min_num;
                    $cg_data['p2_course_max_num'] =$p2_course_max_num;
                    $cg_data['p2_gift_min_num'] =$p2_gift_min_num;
                    $cg_data['p2_gift_max_num'] =$p2_gift_max_num;

                    if ($_S['bottom_jiage']==2){
                        $p3_course_min_num = $_P['p3_course_min_num'];
                        $p3_course_max_num = $_P['p3_course_max_num'];
                        $p3_gift_min_num = $_P['p3_gift_min_num'];
                        $p3_gift_max_num = $_P['p3_gift_max_num'];
                        if ($p3_course_min_num>$p3_course_max_num){
                            show_json(0,'拼团③可选课程数不合法');
                            return;
                        }
                        if ($p3_gift_min_num>$p3_gift_max_num){
                            show_json(0,'拼团③可选赠品数不合法');
                            return;
                        }
                        $cg_data['p3_course_min_num'] =$p3_course_min_num;
                        $cg_data['p3_course_max_num'] =$p3_course_max_num;
                        $cg_data['p3_gift_min_num'] =$p3_gift_min_num;
                        $cg_data['p3_gift_max_num'] =$p3_gift_max_num;
                    }
                }
                if (!$item){
                    $cg_data['themeid']=$themeid;
                    $res = BeilaiModel::insertReward($cg_data);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],$cg_data);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //成团奖励设置
            if ($op=='tuan_setting'){
                $gift_id = $_P['gift_id'];
                if (!$gift_id){
                    show_json(0,'成团奖励选择不正确');
                    return;
                }
                $gift = BeilaiModel::getGiftItemById($themeid,$gift_id);
                $data = [
                    ['key'=>'成团解锁','val'=>'团长专属','type'=>'txt'],
                ];
                $temp_arr1 = ['key'=>'奖品','val'=>$gift['name'],'type'=>'txt'];
                $temp_arr2 = ['key'=>'提供商家','val'=>$gift['shangjia_name'],'type'=>'txt'];
                $temp_arr3 = ['key'=>'主图','val'=>$gift['top_img'],'type'=>'img'];
                $temp_arr4 = ['key'=>'价值','val'=>$gift['price'],'type'=>'txt'];
                $temp_arr5 = ['key'=>'奖励描述','val'=>$gift['detail'],'type'=>'txt'];
                $temp_arr6 = ['key'=>'id','val'=>$gift['id'],'type'=>'txt'];
                array_push($data,$temp_arr1);
                array_push($data,$temp_arr2);
                array_push($data,$temp_arr3);
                array_push($data,$temp_arr4);
                array_push($data,$temp_arr5);
                array_push($data,$temp_arr6);
                if (!$item){
                    $res = BeilaiModel::insertReward(['tuan'=>json_encode($data),'themeid'=>$themeid]);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['tuan'=>json_encode($data)]);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //删除成团奖励设置
            if ($op=='del_tuan'){
                $res = BeilaiModel::editReward(['themeid'=>$themeid],['tuan'=>'']);
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //新增解锁奖
            if ($op=='invite_people'){
                $invite_people = intval($_P['invite_people']);
                $invite_desc = trim($_P['invite_desc']);
                $gift_id = intval($_P['gift_id']);
                if (!$invite_people || $invite_people>30){
                    show_json(0,'邀请人数不合法');
                    return;
                }
                if (!$gift_id){
                    show_json(0,'解锁奖品不合法');
                    return;
                }
                if (!$item){
                    $order_list = [];
                }else{
                    if (!$item['order']){
                        $order_list = [];
                    }else{
                        $order_list = json_decode($item['order'],true);
                        if (count($order_list)>=5){
                            show_json(0,'达到解锁奖励上限');
                            return;
                        }
                    }
                }
                $gift_item = BeilaiModel::getGiftItemById($themeid,$gift_id);
                $temp_arr = ['invite'=>$invite_people,'invite_desc'=>$invite_desc,'img'=>$gift_item['top_img'],
                    'name'=>$gift_item['name'],'price'=>$gift_item['price'],'gift_id'=>$gift_item['id'],
                    'shangjia_name'=>$gift_item['shangjia_name']];
                array_push($order_list,$temp_arr);
                if (!$item){
                    $res = BeilaiModel::insertReward(['order'=>json_encode($order_list),'themeid'=>$themeid]);
                }else{
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['order'=>json_encode($order_list)]);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //修改解锁奖
            if ($op=='invite_people_edit'){
                $key = $_P['key'];
                if (!$item || ($item && !$item['order'])){
                    show_json(0,'非法操作');
                    return;
                }
                $invite_people = intval($_P['invite_people']);
                $invite_desc = trim($_P['invite_desc']);
                $gift_id = intval($_P['gift_id']);
                if (!$invite_people || $invite_people>30){
                    show_json(0,'邀请人数不合法');
                    return;
                }
                if (!$gift_id){
                    show_json(0,'解锁奖品不合法');
                    return;
                }
                $gift_item = BeilaiModel::getGiftItemById($themeid,$gift_id);
                $temp_arr = ['invite'=>$invite_people,'invite_desc'=>$invite_desc,'img'=>$gift_item['top_img'],
                    'name'=>$gift_item['name'],'price'=>$gift_item['price'],'gift_id'=>$gift_item['id'],
                    'shangjia_name'=>$gift_item['shangjia_name']];

                $order_list = json_decode($item['order'],true);
                if (is_array($order_list)){
                    foreach ($order_list as $k => $val)
                    {
                        if ($key==$k){
                            $order_list[$k]=$temp_arr;
                        }
                    }
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['order'=>json_encode($order_list)]);
                    if (!$res){
                        show_json(0,['msg'=>'操作失败']);
                    }else{
                        show_json(1,['msg'=>'操作成功']);
                    }
                }
            }
            //删除解锁奖
            if ($op=='del_order_item'){
                $key = $_P['key'];
                if (!$item || ($item && !$item['order'])){
                    show_json(0,'非法操作');
                    return;
                }
                $order_list = json_decode($item['order'],true);
                if (is_array($order_list)){
                    foreach ($order_list as $k => $val)
                    {
                        if ($key==$k){
                            unset($order_list[$k]);
                        }
                    }
                    $res = BeilaiModel::editReward(['themeid'=>$themeid],['order'=>json_encode($order_list)]);
                    if (!$res){
                        show_json(0,['msg'=>'操作失败']);
                    }else{
                        show_json(1,['msg'=>'操作成功']);
                    }
                }
            }
            //增加\修改渠道优惠券
            if ($op=='add_edit_coupon'){
                $id = $_P['coupon_id'];
                $name = $_P['name'];
                $price = $_P['price'];
                $qty = $_P['qty'];
                $get_start_time = $_P['get_time']['start'];
                $get_end_time = $_P['get_time']['end'];
                $use_start_time = $_P['use_time']['start'];
                $use_end_time = $_P['use_time']['end'];
                $shangjiaid = $_P['shangjiaid'];
                if (!$id){
                    if (!$name || ($name && mb_strlen($name, 'utf-8')>20)){
                        show_json(0,'优惠券名称不合法,限制20字以内');
                        return;
                    }
                    if (!$price){
                        show_json(0,'优惠券金额不合法');
                        return;
                    }
                    if (!$qty){
                        show_json(0,'优惠券数量不合法');
                        return;
                    }
                }

                if ($use_start_time<$get_start_time){
                    show_json(0,'优惠券使用时间不可大于领取时间');
                    return;
                }
                //判断多价格优惠券金额是否合法
                if ($_S['bottom_jiage']>0){
                    if (isset($_P['bind_price']) && $_P['bind_price']==1 && (float)$price>=(float)$_C['groupprice']){
                        show_json(0,'优惠券金额大于拼团价①，请修改');
                        return;
                    }
                    if (isset($_P['bind_price']) && $_P['bind_price']==2 && (float)$price>=(float)$_C['groupprice2']){
                        show_json(0,'优惠券金额大于拼团价②，请修改');
                        return;
                    }
                    if (isset($_P['bind_price']) && $_P['bind_price']==3 && (float)$price>=(float)$_C['groupprice3']){
                        show_json(0,'优惠券金额大于拼团价③，请修改');
                        return;
                    }
                }
                //判断每个商家最多设置几张优惠券
                $shangjia_coupon_list = db_get(t_shangjia_coupon,['shangjiaid'=>$shangjiaid,'status'=>1]);
                if ($_S['bottom_jiage']==0){
                    if (count($shangjia_coupon_list)>=1){
                        show_json(0,'每个商家最多设置1张优惠券，如需修改请下架当前商家优惠券后编辑');
                        return;
                    }
                }else{
                    //判断绑定价格的是否存在已上架优惠券
                    $bind_price_coupon_item = db_get(t_shangjia_coupon,['shangjiaid'=>$shangjiaid,'status'=>1,'bind_price'=>$_P['bind_price']]);
                    if ($bind_price_coupon_item){
                        show_json(0,'当前商家已存在绑定拼团价的优惠券，请检查后重新编辑');
                        return;
                    }
                    if ($_S['bottom_jiage']==1){
                        if (count($shangjia_coupon_list)>=2){
                            show_json(0,'每个商家最多设置2张优惠券，如需修改请下架当前商家优惠券后编辑');
                            return;
                        }
                    }else{
                        if (count($shangjia_coupon_list)>=3){
                            show_json(0,'每个商家最多设置3张优惠券，如需修改请下架当前商家优惠券后编辑');
                            return;
                        }
                    }
                }


                if (!$id){
                    $data = [
                        'shangjiaid'=>$shangjiaid,
                        'name'=>$name,
                        'price'=>$price,
                        'qty'=>$qty,
                        'status'=>0,
                        'get_start_time'=>$get_start_time,
                        'get_end_time'=>$get_end_time,
                        'use_start_time'=>$use_start_time,
                        'use_end_time'=>$use_end_time,
                        'themeid'=>$themeid,
                    ];
                    if ($_S['bottom_jiage']>0){
                        if (isset($_P['bind_price']) && $_P['bind_price']){
                            $data['bind_price'] = $_P['bind_price'];
                        }
                    }
                    //判断是否存在相关的价格以及商家的优惠券
                    $tmp_where = ['shangjiaid'=>$shangjiaid,'bind_price'=>1,'themeid'=>$themeid];
                    if ($data['bind_price']){
                        $tmp_where['bind_price'] = $data['bind_price'];
                    }
                    $check_flag = db_get(t_shangjia_coupon,$tmp_where);
                    if ($check_flag){
                        show_json(0,'存在重复记录，请检查后直接修改');
                        return;
                    }
                    $res =BeilaiModel::insertCoupon($data);
                }else{
                    $where = ['id'=>$id];
                    $data = [
                        'name'=>$name,
                        'price'=>$price,
                        'qty'=>$qty,
                        'get_start_time'=>$get_start_time,
                        'get_end_time'=>$get_end_time,
                        'use_start_time'=>$use_start_time,
                        'use_end_time'=>$use_end_time,
                    ];
                    if ($_S['bottom_jiage']>0){
                        if (isset($_P['bind_price']) && $_P['bind_price']){
                            $data['bind_price'] = $_P['bind_price'];
                        }
                    }
                    $res = BeilaiModel::editCouponItem($where,$data);
                }
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //获取渠道优惠券
            if ($op=='get_coupon'){
                $id = $_P['id'];
                $res = BeilaiModel::getCoupon($themeid,$id);
                if (!$res){
                    show_json(0,['msg'=>'优惠券不存在']);
                }else{
                    show_json(1,['msg'=>'操作成功','data'=>$res]);
                }
            }
            //删除渠道优惠券
            if ($op=='del_coupon'){
                $id = $_P['id'];
                $where = ['id'=>$id];
                $res1 = BeilaiModel::getCoupon($themeid,$id);
                if ($res1 && $res1['get_num']){
                    show_json(0,['msg'=>'用户领取的优惠券不可删除']);
                }
                $res = BeilaiModel::delCouponItem($where);
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            //修改优惠券的库存
            if($op=='edit_coupon_qty'){
                $id = $_P['coupon_id'];
                $action = $_P['action'];
                $res = BeilaiModel::getCoupon($themeid,$id);
                $op_qty = $_P['op_qty'];
                $new_qty = 0;
                if ($action=='add'){
                    $new_qty = $res['qty']+$op_qty;
                }else{
                    $new_qty = max(0,($res['qty']-$op_qty));
                    if (($res['get_num']+5)>=$new_qty){
                        show_json(1,['msg'=>'到达库存预警不可减少']);
                    }
                }
                $where = ['id'=>$id];
                $data = [
                    'qty'=>$new_qty,
                ];
                $res = BeilaiModel::editCouponItem($where,$data);
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    show_json(1,['msg'=>'操作成功']);
                }
            }
            if ($op=='edit_coupon_status'){
                $id = $_P['id'];
                $res = BeilaiModel::getCoupon($themeid,$id);
                if (!$res){
                    show_json(0,['msg'=>'操作失败']);
                }else{
                    if ($res['status']==1 || $res['status']==2){
                        $status = 0;
                    }else{
                        $status = 1;
                        //判断绑定价格的是否存在已上架优惠券
                        $bind_price_coupon_item = db_get(t_shangjia_coupon,['shangjiaid'=>$res['shangjiaid'],'status'=>1,'bind_price'=>$res['bind_price']]);
                        if ($bind_price_coupon_item){
                            show_json(0,['msg'=>'当前商家已存在绑定拼团价的优惠券，请检查后重新编辑']);
                            return;
                        }
                    }
                    $where = ['id'=>$id];
                    $data = ['status'=>$status];
                    $res = BeilaiModel::editCouponItem($where,$data);
                    if (!$res){
                        show_json(0,['msg'=>'操作失败']);
                    }else{
                        show_json(1,['msg'=>'操作成功']);
                    }
                }

            }
        }
    }
}